// pages/login/login.js
var app=getApp();
Page({
    data:{
        url:'',
        loginInf:{
            username:'',
            password:''
        },
        userInf:{}
    },
    onLoad(options) {
        // 获取全局的url赋值给当前页面的URL变量
        this.setData({
            url:app.globalData.URL
         })
    },
    
    unChange: function (e) {
        this.setData({
          'loginInf.username' : e.detail.value
        });
        if(this.data.loginInf.username==null||this.data.loginInf.username==''){
            this.setData({
                'loginInf.password' : null
            });
        }
      },
    pdChange: function (e) {
        this.setData({
            'loginInf.password' : e.detail.value
        });
      }, 

    toMine(){
        wx.switchTab({
            url: '/pages/mine/mine',
        })
    },
    login() {
        
        let that = this
        //console.log(this.data.loginInf);
        if(that.data.loginInf.username==''||that.data.loginInf.username==null){
            that.setData({
                'loginInf.username' : ' '
            })
        }
        if(that.data.loginInf.password==''||that.data.loginInf.password==null){
            that.setData({
                'loginInf.password' : null
            })
        }
        wx.request({
            url: that.data.url + 'user/login', // 替换为实际的请求地址
            method: 'POST',
            data: that.data.loginInf,
            header: {
                'content-type': 'application/json' // 设置请求头，指定数据格式为 JSON
            },
            success: function (res) {
                //console.log('请求成功', res);
                if(res.data.code==1){
                    //console.log(111);
                    //console.log(res);
                    //console.log(res.data.data);
                    that.setData({
                        'userInf' :res.data.data
                    })
                    wx.setStorage({
                        key: 'loginInfo',
                        data: that.data.userInf,
                        success: function () {
                            app.globalData.loginNum = 1
                            wx.switchTab({
                                url: '/pages/mine/mine',
                            })
                            //console.log('登录成功');
                        },
                        fail: function (err) {
                            console.error('登录信息存储失败', err);
                        }
                    })
                }else{
                    wx.showModal({
                        title: '登陆失败',
                        content: '用户名或密码错误，请重试！',
                        success: function (res) {
                        if (res.confirm) {
                            that.setData({
                                'loginInf.username' : '',
                                'loginInf.password' : ''
                              });
                        } else {
                            wx.redirectTo({
                                url: '/pages/mine/mine',
                                })
                            }
                        }
                    });
                }
                wx.getStorage({
                    key: 'loginInfo',
                    success(res) {
                        //console.log('获取到的字符串数据:', res.data);
                        app.globalData.userInfor = res.data
                        //console.log(app.globalData.userInfor);     
                    },
                })
            },
            fail: function (err) {
                console.error('请求失败', err);
            }
        });
    },

        goRegister(){
            wx.navigateTo({
              url: '/pages/mine/register/register',
            })
        }
})